﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace ElMexicanos.Base.Models
{
    class AnimSprite : Sprite
    {

        protected AnimDefinition Definition;
        protected Point CurrentFrame;
        protected bool FinishedAnimation = false;
        protected double TimeBetweenFrame = 16; // 60 fps 
        protected double lastFrameUpdatedTime = 0;

        private int _Framerate = 60;
        public int Framerate
        {
            get { return this._Framerate; }
            set
            {
                if (value <= 0)
                    throw new ArgumentOutOfRangeException("Framerate can't be less or equal to 0");
                if (this._Framerate != value)
                {
                    this._Framerate = value;
                    this.TimeBetweenFrame = 1000.0d / (double)this._Framerate;
                }
            }
        }


        public AnimSprite(AnimDefinition definition) 
            : base() 
        {
            this.Definition = definition;
            this.CurrentFrame = new Point();
            this.position = new Vector2();
        }

        public AnimSprite(AnimDefinition definition, Vector2 position)
            : base()
        {
            this.Definition = definition;
            this.CurrentFrame = new Point();
            this.position = position;
        }

        public void Initialize()
        {
            this.Framerate = this.Definition.FrameRate;
        }

        public void Reset()
        {
            this.CurrentFrame = new Point();
            this.FinishedAnimation = false;
            this.lastFrameUpdatedTime = 0;
        }

        public void LoadContent()
        {
            texture = RessourcesManager.GetInstance().GetTexture(this.Definition.AssetName);
        }


        public void Update(GameTime time)
        {
            if (FinishedAnimation) return;

            this.lastFrameUpdatedTime += time.ElapsedGameTime.Milliseconds;

            if (this.lastFrameUpdatedTime > this.TimeBetweenFrame)
            {
                this.lastFrameUpdatedTime = 0;
                if (this.Definition.Loop)
                {
                    this.CurrentFrame.X++;
                    if (this.CurrentFrame.X >= this.Definition.NbFrames.X)
                    {
                        this.CurrentFrame.X = 0;
                        this.CurrentFrame.Y++;
                        if (this.CurrentFrame.Y >= this.Definition.NbFrames.Y)
                            this.CurrentFrame.Y = 0;
                    }
                }
                else
                {
                    this.CurrentFrame.X++;
                    if (this.CurrentFrame.X >= this.Definition.NbFrames.X)
                    {
                        this.CurrentFrame.X = 0;
                        this.CurrentFrame.Y++;
                        if (this.CurrentFrame.Y >= this.Definition.NbFrames.Y)
                        {
                            this.CurrentFrame.X = this.Definition.NbFrames.X - 1;
                            this.CurrentFrame.Y = this.Definition.NbFrames.Y - 1;
                            this.FinishedAnimation = true;
                        }
                    }
                }
            }
        }


        public void Draw(SpriteBatch spriteBatch, GameTime time)
        {
            spriteBatch.Draw(this.texture,
                                  new Rectangle((int)this.position.X, (int)this.position.Y, this.Definition.FrameSize.X, this.Definition.FrameSize.Y),
                                  new Rectangle(this.CurrentFrame.X * this.Definition.FrameSize.X, this.CurrentFrame.Y * this.Definition.FrameSize.Y, this.Definition.FrameSize.X, this.Definition.FrameSize.Y),
                                  Color.White);
        }

    }
}
